//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "FpsMaterial.h"
#include "SceneManager/FpsCommonGLMaterialRenderer.h"

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

using namespace irr;
using namespace collada;
using namespace video;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool FpsMaterial::s_ForceModifyTextureLodBias = false;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

inline const char* Mtrl_GetNumberFromString(const char* str, int& N)
{
	N = 0;

	while(*str && *str != '_')
	{
		N = (10 * N) + (*str - '0');
		str++;
	}

	return str;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void FpsMaterial::postProcessMaterial(IRootSceneNode *pRoot)
{
	video::SMaterial&	mat				=	DriverFormatMaterial;
	collada::SEffect*	colladaFx		=	OriginalMaterial.instanceEffect.pEffect;
	const char*			pName			=	colladaFx->name;

	// default settings - anable fog, disable lightning
	mat.setFlag(irr::video::EMF_FOG_ENABLE,	true);
	mat.setFlag(irr::video::EMF_LIGHTING,	false);	

	if(pName)
	{
		const char* pNewMaterialStr = strstr(pName, "___");
		if(pNewMaterialStr)
		{
			int mtrlFlags		= 0;
			int renderPriority	= 0;
			int mtrlRenderer	= 0;

			while(*pNewMaterialStr == '_')
				pNewMaterialStr++;

			//pNewMaterialStr += 3; // skip '---'
			if(*pNewMaterialStr)
				pNewMaterialStr = Mtrl_GetNumberFromString(pNewMaterialStr, mtrlFlags);

			if(*pNewMaterialStr)
			{
				pNewMaterialStr += 1; // skip '-'
				if(*pNewMaterialStr)
					pNewMaterialStr = Mtrl_GetNumberFromString(pNewMaterialStr, renderPriority);
			}

			if(*pNewMaterialStr)
			{
				pNewMaterialStr += 1; // skip '-'
				if(*pNewMaterialStr)
					pNewMaterialStr = Mtrl_GetNumberFromString(pNewMaterialStr, mtrlRenderer);
			}

			const int FPS_MTRL_FLAG_ALPHA_TEST		= 1;
			const int FPS_MTRL_FLAG_BACK_CULL		= 2;
			const int FPS_MTRL_FLAG_Z_WRITE			= 4;
			const int FPS_MTRL_FLAG_Z_READ			= 8;
			const int FPS_MTRL_FLAG_FOG				= 16;

			// Material Flags
			{
				mat.setFlag(irr::video::EMF_BACK_FACE_CULLING,	(mtrlFlags & FPS_MTRL_FLAG_BACK_CULL)	!= 0);
				mat.setFlag(irr::video::EMF_ZWRITE_ENABLE,		(mtrlFlags & FPS_MTRL_FLAG_Z_WRITE)		!= 0);
				mat.setFlag(irr::video::EMF_ZBUFFER,			(mtrlFlags & FPS_MTRL_FLAG_Z_READ)		!= 0);
				mat.setFlag(irr::video::EMF_FOG_ENABLE,			(mtrlFlags & FPS_MTRL_FLAG_FOG)			!= 0);
			}

			// Material Renderer
			{
				// additive
				switch(mtrlRenderer)
				{
				case 1:
					mat.setMaterialType(EMT_NONTRANSPARENT_ADDITIVE_REPLACE);
					if(renderPriority != 1.f)
					{
						renderPriority = FPS_MATERIAL_RENDER_PRIORITY_ADDITIVE;
					}
					break;
				case 2:
					mat.setMaterialType(EMT_NONTRANSPARENT_ADDITIVE_MODULATE);
					if(renderPriority != 1.f)
					{
						renderPriority = FPS_MATERIAL_RENDER_PRIORITY_ADDITIVE;
					}
					break;
				}
			}

			// Render Priority - the priority of the material is set in MaterialTypeParam2
			{
				mat.setMaterialTypeParam(2, (float)renderPriority);

				if(mat.getMaterialTypeParam(2) == 2.f)
				{
					if(mat.getMaterialType() == video::EMT_TRANSPARENT_ALPHA_CHANNEL)
						mat.setMaterialTypeParam(2, FPS_MATERIAL_RENDER_PRIORITY_GROUND_TRANSPARENT);
					else
						mat.setMaterialTypeParam(2, FPS_MATERIAL_RENDER_PRIORITY_GROUND);
				}
				else if(mat.getMaterialTypeParam(2) == 1.f)
				{
					if(mat.getMaterialType() != EMT_NONTRANSPARENT_ADDITIVE_REPLACE &&
						mat.getMaterialType() != EMT_NONTRANSPARENT_ADDITIVE_MODULATE)
					{
						mat.setMaterialType(EMT_NONTRANSPARENT_ADDITIVE_REPLACE);
					}
					mat.setMaterialTypeParam(2, FPS_MATERIAL_RENDER_PRIORITY_REFLECTIONS);
				}

				if(mtrlFlags & FPS_MTRL_FLAG_ALPHA_TEST)
				{
					mat.setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);
				}
			}
		}

		mat.setFlag(EMF_FOG_ENABLE, false);

		// temp test
		/*if (mat.getMaterialType() == video::EMT_TRANSPARENT_ALPHA_CHANNEL)
			mat.setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF);*/
	}

	if(s_ForceModifyTextureLodBias)
	{
		for(int i = 0; i < MATERIAL_MAX_TEXTURES; i++)
		{
			ITexture* tex = mat.getTexture(i);

			if(tex && tex->hasMipMaps())
			{
				tex->setLODBias(-2);
				tex->setMinFilter(ETFT_NEAREST_MIPMAP_NEAREST);
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
